<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            display: block;
            margin-top: 20px;
            border-radius: 10px;
            margin-left: 600px;
            width: 300px;
            height: 30px;
            border: none;
            background-color: lightgray;
            padding-left: 10px;
            box-sizing: border-box;
        }

        button {
            display: block;
            margin-top: 20px;
            border-radius: 10px;
            margin-left: 600px;
            width: 300px;
            height: 30px;
            border: none;
            background-color: lightblue;
        }
    </style>
</head>

<body>

</body>
<input type="text" placeholder="请输入账号">
<input type="password" placeholder="请输入密码">
<input type="password" placeholder="确认密码">
<button>注册</button>

</html>
<script>
    //对象转查询字符串
    function objToSearchString(obj) {
        var str = ''
        for (var key in obj) {
            str += `${key}=${obj[key]}&`;
        }
        return str.slice(0, str.length - 1);
    }

    //封装ajax函数
    function ajax(method, url, params) {
        //new一个Promise对象并返回
        return new Promise((resolve, reject) => {
            //1.创建请求对象
            var xhr = new XMLHttpRequest()
            //2.配置请求对象
            xhr.open(method, method == 'get' ? url + "?" + objToSearchString(params) : url);
            //3.设置回调函数
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    //cb( JSON.parse(xhr.responseText) )
                    resolve(JSON.parse(xhr.responseText));
                    //resolve会更改Promise对象的状态为 成功, 同时会立即调用then的回调函数
                }
            }
            //4.发送请求
            //先对post请求设置 content-type 请求头
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send(method == 'get' ? null : objToSearchString(params));
        })
    }
    var inEle = document.querySelectorAll('input')
    var btn = document.querySelector('button')
    btn.onclick = function () {
        if (inEle[0].value && inEle[1].value) {
            if (/^\d{4}$/.test(inEle[0].value) && /^\d{4}$/.test(inEle[1].value) && /^\d{4}$/.test(inEle[2].value)) {
                if (inEle[1].value == inEle[2].value) {
                    ajax('get', 'https://yantianfeng.com/api/login', { username: inEle[0].value, password: inEle[1].value, passwords: inEle[2].value }).then((data) => {
                        console.log(data);
                        if (data.status == 200) {
                            console.log('登陆成功');
                        } else {
                            console.log('登陆失败!');
                        }
                    })
                } else {
                    alert('两次密码不一致')
                }
            } else {
                alert('请输入正确的手机号/密码')
            }
        } else {
            alert('不能为空')
        }
    }
</script>